package com.example.picturemodule

import android.graphics.Matrix
import android.os.Bundle
import android.view.View
import android.widget.EditText
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity

/*
   Matrix ，中文里叫矩阵，高等数学里有介绍，在图像处理方面，主要是用于平面的缩放、平移、旋转等操作
   
*/
class MatrixTestActivity : AppCompatActivity() {
	private var et_matrix_scale: EditText? = null
	private var et_matrix_rotate: EditText? = null
	private var et_matrix_translateX //偏移量X
			: EditText? = null
	private var et_matrix_translateY //偏移量Y
			: EditText? = null
	private var iv_matrix_icon: ImageView? = null
	private var matrix: Matrix? = null
	override fun onCreate(savedInstanceState: Bundle?) {
		super.onCreate(savedInstanceState)
		setContentView(R.layout.activity_matrix)
		et_matrix_scale = findViewById<View>(R.id.et_matrix_scale) as EditText
		et_matrix_rotate = findViewById<View>(R.id.et_matrix_rotate) as EditText
		et_matrix_translateX = findViewById<View>(R.id.et_matrix_translateX) as EditText
		et_matrix_translateY = findViewById<View>(R.id.et_matrix_translateY) as EditText
		iv_matrix_icon = findViewById<View>(R.id.iv_matrix_icon) as ImageView
		matrix = Matrix()
	}

	fun scaleBitmap(view: View?) {
		val scale = et_matrix_scale!!.text.toString().toFloat()
		//保存缩放比例
		matrix!!.postScale(scale, scale)
		//将matrix设置到imageView
		iv_matrix_icon!!.imageMatrix = matrix
	}

	fun rotateBitmap(view: View?) {
		val degree = et_matrix_rotate!!.text.toString().toFloat()
		//保存旋转角度
		matrix!!.postRotate(degree)
		//将matrix设置到imageView
		iv_matrix_icon!!.imageMatrix = matrix
	}

	fun translateBitmap(view: View?) {
		val dx = et_matrix_translateX!!.text.toString().toFloat()
		val dy = et_matrix_translateY!!.text.toString().toFloat()
		//保存平移数据
		matrix!!.postTranslate(dx, dy)
		//将matrix设置到imageView
		iv_matrix_icon!!.imageMatrix = matrix
	}

	fun clearMatrix(view: View?) {
		matrix!!.reset()
		//将matrix设置到imageView
		iv_matrix_icon!!.imageMatrix = matrix
	}
}